/*
 * Main.fx
 *
 * Created on 10.03.2010, 9:27:26
 */

//
package javafx191labs.agordeev.lab02;

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.CustomNode;
import javafx.scene.Node;
import javafx.scene.shape.Circle;
import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import javafx.scene.Group;
import javafx.scene.input.KeyEvent;
import javafx.scene.shape.Rectangle;


/**
 * @author r4diantq
 */

var game : Game = Game{};

var timer : Timeline = Timeline {
    repeatCount: Timeline.INDEFINITE
    keyFrames :
        KeyFrame {
            time : 26ms
            action : function() : Void {
                //char.jump();
                game.char.forward();
                game.char.back();
                game.char.jump();
                //game.char.fall();
                game.collision();
            }
        }
};

Stage {
    title: "Application title"
    scene: Scene {
        width: 640
        height: 480
        content: [game]
    }
}
timer.play();

class Game extends CustomNode{
    public var isIntersecting = false;
    public var char : Char = Char{
                       posX: 100,
                       posY: 100,
                       //isJumping: true,
                       radius: 20,
                       jumpDist: 30,
                       accelerationX: 7,
                       accelerationY: 7,
                       velocityY: 1.1
    };

/*
    public var ground : Ground[] = Ground{
                        posX: 0;
                        posY: 500;
    };
*/
    public var ground : Ground[];

    public var group : Group = Group{
        content:[char,
                 ground[0] = Ground{
                     posX: 0;
                     posY: 200;
                 },
                 ground[1] = Ground{
                     posX: 50;
                     posY: 400;
                 },
                 ground[2] = Ground{
                     posX: 0;
                     posY: 600;
                 }


                ]
        focusTraversable: true;
        onKeyPressed: function(e:KeyEvent) : Void{
            if(e.text == "Space"){
                char.isJumping = true;
            }
            if(e.text == "A"){
                char.isBacking = true;
            }
            if(e.text == "D"){
                char.isForwarding = true;
            }

        }
        onKeyReleased: function(e:KeyEvent) : Void{
            if(e.text == "Space"){
                //char.isJumping = false;
            }
            if(e.text == "A"){
                char.isBacking = false;
            }
            if(e.text == "D"){
                char.isForwarding = false;
            }
        }
    }

    public function collision(){
        for(g in ground){
            //char.isJumping = true;
            //char.isFalling = true;
            //if(g.intersects(char.posX + 20, char.posX + 20, 40, 40)){

            if(g.intersects(char.posX, char.posY, char.radius, char.radius)){
                char.isJumping = false;
                char.isFalling = false;
                char.temp_dist = char.jumpDist;
                isIntersecting = true;
            } else if((isIntersecting == false) and (g.intersects(char.posX, char.posY, char.radius, char.radius) != true)){
                if(char.isJumping == false){
                    char.isFalling = true;
                    char.accelerationY = 2;
                    char.fall();
                }
            } else if(char.isFalling == false){
                if(g.intersects(char.posX, char.posY, char.radius, char.radius)){
                    isIntersecting = true;
                } else{
                    char.isFalling = true;
                }

//            } else if(char.isFalling == true and isIntersecting == true){
  //              char.isFalling = false;
            } else{
                isIntersecting = false;
            }

        }
    }

    override protected function create () : Node {
        return group;
    }
};


class Char extends CustomNode{
    public var posX : Number;
    public var posY : Number;
    public var radius : Number;

    public var isJumping : Boolean;
    public var isForwarding : Boolean;
    public var isBacking : Boolean;

    public var isFalling : Boolean;

    public var jumpDist : Number;
    public var accelerationX : Number;
    public var accelerationY : Number;
    public var velocityX : Number;
    public var velocityY : Number;

    protected var temp_dist : Number = jumpDist;

    public function jump(){

        if(isJumping){
            if(temp_dist > 0){
                temp_dist -= velocityY;
                posY -= accelerationY;
            }
            else{
                //posY += accelerationY;
                isFalling = true;
                fall();
            }
        }
    }

    public function forward(){
        if(isForwarding){
            posX += accelerationX;
        } else{
            posX = posX;
        }


    }

    public function back(){
        if(isBacking){
            posX -= accelerationX;
        } else{
            posX = posX;
        }


    }

    public function fall(){
        if(isFalling){
            posY += accelerationY;
        } else{
            posY = posY;
        }


    }


    override protected function create () : Node {
        Circle{
            centerX : bind posX;
            centerY : bind posY;
            radius  : bind radius;
        }

    }
};

class Ground extends CustomNode{
    public var posX : Number;
    public var posY : Number;
    override protected function create () : Node {
        Rectangle{
            x: bind posX;
            y: bind posY;
            width: 1000;
            height: 10;
        }

    }
}
